package com.bys.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.service.ParameterType;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;
@Configuration
@EnableSwagger2
public class Knife4jConfig {
    private static List<Parameter> getTokenParam() {
        List<Parameter> pars = new ArrayList<>();
        ParameterBuilder tokenParam = new ParameterBuilder();
        Parameter token = tokenParam.name("token")
                .defaultValue("")
                .description(" 用户的 token")
                .modelRef(new ModelRef("string"))
                .parameterType(ParameterType.HEADER)
                .required(false)
                .build();

        pars.add(token);
        return pars;
    }
    @Bean
    public Docket systemApiconfig(){
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("systemApiInfo")
                .apiInfo(golbalApiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.bys.system"))
                .paths(PathSelectors.regex("/system/.*"))
                .build()
                .globalOperationParameters(getTokenParam());

    }

    private ApiInfo golbalApiInfo() {
        return new ApiInfoBuilder().title(" 花海后台管理系统 ")
                .description(" 描述后台管理系统的 接口")
                .version("1.0")
                .contact(new Contact("bys","http://bys.com","13403504277@163.com"))
                .build();
    }
}
